$(function(){
	var isEdit = $("#paramFrm input[name='isEdit']").val();
	if(isEdit == null || isEdit == 'false' || isEdit == false){
		$("#cateDetailFrm .statuDiv").hide();
	}
	
	$("#category_name").keyup(function(){
		limit(this,20);//20字节内
		})
});
var oldRoles = null;
var oldGroups = null;
var ajaxE = {
	onQueryGroupSucc:function(data){
		if(data.success == true){
			
		}
	},
	findOneSucc:function(data){
		if(data.success == true){
			data.info.status = (data.info.status == 0)?0:1;
			$("#cateDetailFrm").autofill(data.info);
			//添加已拥有角色至div显示
			/*oldRoles = data.info.category_role;
			var html = '';
			$.each(oldRoles,function(index,item){
				if(item != null){
					html += '<p class="itemDiv" name="'+item.role_id+'">'+
							'<input style="display:none" value="'+item.role_id+'">'+
							'<span class="roleshow label label-warning">'+item.role_name+
								'<a href="javascript:;" title="删除" onclick="removeRole(\''+item.role_id+'\')">'+
								'<i class="icon icon-times"></i></a></span>'+
						'</p>';
				}
			});
			$(".roleShowDiv").empty();
			$(".roleShowDiv").append(html);*/
			oldGroups = data.info.category_group;
			html = '';
			$.each(oldGroups,function(index,item){
				if(item != null){
					html += '<p class="itemDiv" name="'+item.group_id+'">'+
					'<input style="display:none" value="'+item.group_id+'">'+
					'<span class="roleshow label label-warning">'+item.group_name+
						'<a href="javascript:;" title="删除" onclick="removeGroup(\''+item.group_id+'\')">'+
						'<i class="icon icon-times"></i></a></span>'+
				'</p>';
				}
			});
			$(".roleGroupShowDiv").empty();
			$(".roleGroupShowDiv").append(html);
		}
	},
	onSaveSucc:function(data){
		if(data.success == true){
			new $.zui.Messager('保存成功!', {
			    type: 'success',
			    time: 2000,
			    placement:'center'
			}).show();
			categoryLstTable.refresh();
		}
	}
}

var tableO = {
	onGroupRender:function(colName,text){
		switch (colName) {
		case 'status':
			var txt = (text == '0' || text == 0)?"禁用":"正常";
			var color = (text == '0' || text == 0)?"red":"green";
			return '<span style="color:'+color+'">'+txt+'</span>';
		case 'create_time':
			/*var html = '<div>'+
					'<button class="btn kbtn tableDelbtn groupBtn">添加</button>'+
				'</div>';*/
			var html = '<div>'+
			'<a class="groupBtn" href="javascript:;" title="添加资源组">'+
				'<i class="icon icon-hand-right"></i></a>'+
			'</div>';
			return html;
		}
	},
	onRenderItem:function(colName,text){
		switch(colName){
		case 'state':
			var txt = (text == '0' || text == 0)?"禁用":"正常";
			var color = (text == '0' || text == 0)?"red":"green";
			return '<span style="color:'+color+'">'+txt+'</span>';
		case 'role_type':
			var html = '<div>'+
				'<button class="btn kbtn tableDelbtn otherBtn">添加</button>'+
				'</div>';
			return html;
		}
	},
	onGroupReady:function(){
		$(".groupBtn").click(function(e){
			var num = $(this).parents('tr').index();
			var record = categoryGroupTable.rows[num];
			var span = $(".roleGroupShowDiv p[name='"+record.data[0]+"']");
			if(span == undefined || span == null || span.length == 0){
				//添加至div显示
				var html = '<p class="itemDiv" name="'+record.data[0]+'">'+
				'<input style="display:none" value="'+record.data[0]+'">'+
				'<span class="roleshow label label-warning">'+record.data[1].text+
					'<a href="javascript:;" title="删除" onclick="removeGroup(\''+record.data[0]+'\')">'+
					'<i class="icon icon-times"></i></a></span>'+
				'</p>';
				$(".roleGroupShowDiv").append(html);
			}
			e.stopPropagation();
			e.preventDefault();
		});
	},
	onAfterReady:function(){
		/*$(".otherBtn").click(function(e){
			var num = $(this).parents('tr').index();
			var record = categoryRolesTable.rows[num];
			var span = $(".roleShowDiv p[name='"+record.data[0]+"']");
			if(span == undefined || span == null || span.length == 0){
				//添加至div显示
				var html = '<p class="itemDiv" name="'+record.data[0]+'">'+
				'<input style="display:none" value="'+record.data[0]+'">'+
				'<span class="roleshow label label-warning">'+record.data[1].text+
					'<a href="javascript:;" title="删除" onclick="removeRole(\''+record.data[0]+'\')">'+
					'<i class="icon icon-times"></i></a></span>'+
				'</p>';
				$(".roleShowDiv").append(html);
			}
			e.stopPropagation();
			e.preventDefault();
		});*/
	},
	onGroupDoubleClick:function(){
//		var curr = categoryGroupTable.getCurrentRecord();
//		$("#paramFrm input[name='group_id']").val(curr.data.group_id);
//		$("#showGroupName").html(curr.data.group_name);
//		cateRolePagination.resetPagination();
//		$("#paramFrm").submit();
	}
}

function saveAll(){
	/*var ids = new Array();
	var inputs = $(".roleShowDiv input");
	$.each(inputs,function(index,item){
		ids.push($(this).val());
	});*/
	var roleChange = false;
	var itemChange = false;
	/*if(oldRoles == null || ids == null){
		if(oldRoles == null && ids != null && ids.length != 0){
			roleChange = true;
		}else if(oldRoles != null && (ids == null || ids.length == 0)){
			roleChange = true;
		}
	}else if(oldRoles.length != ids.length){
		roleChange = true;
	}else{
		for(var i=0;i<oldRoles.length;i++){
			itemChange = true;
			for(var j=0;j<ids.length;j++){
				if(oldRoles[i].role_id == ids[j]){
					itemChange = false;
					break;
				}
			}
			if(itemChange){
				roleChange = true;
				break;
			}
		}
	}
	if(roleChange){
		$("#cateHasRoles").val(ids);
		$("#isRoleChange").val("true");
	}else{
		$("#isRoleChange").val("false");
	}*/
//	$("#cateHasRoles").val(ids);
	roleChange = false;
	itemChange = false;
	//角色组
	var grIds = new Array();
	var groups = $(".roleGroupShowDiv input");
	$.each(groups,function(index,item){
		grIds.push($(this).val());
	});
	if(oldGroups == null || grIds == null){
		if(oldGroups == null && grIds != null && grIds.length != 0){
			roleChange = true;
		}else if(oldGroups != null && (grIds == null || grIds.length == 0)){
			roleChange = true;
		}
	}else if(oldGroups.length != grIds.length){
		roleChange = true;
	}else{
		for(var i=0;i<oldGroups.length;i++){
			itemChange = true;
			for(var j=0;j<grIds.length;j++){
				if(oldGroups[i].group_id == grIds[j]){
					itemChange = false;
					break;
				}
			}
			if(itemChange){
				roleChange = true;
				break;
			}
		}
	}
	if(roleChange){
		$("#cateHasGroups").val(grIds);
		$("#isGroupChange").val("true");
	}else{
		$("#isGroupChange").val("false");
	}
//	$("#cateHasGroups").val(grIds);
	var name = $("#cateDetailFrm input[name='category_name']").val();
	if(name == null || name == '' || name.length == 0){
		new $.zui.Messager('名称不能为空!', {
		    type: 'danger',
		    time: 2000,
		    placement:'center'
		}).show();
		 $("#cateDetailFrm input[name='category_name']").focus();
	}else{
		$("#cateDetailFrm").submit();
	}
}

function removeRole(id){
	$(".roleShowDiv p[name='"+id+"'").remove();
}
function removeGroup(id){
	$(".roleGroupShowDiv p[name='"+id+"'").remove();
}
function closeDialog(){
	MsgBox.closeActiveDialog();
}
//获取字符串的字节长度
function len(s) {
s = String(s);
return s.length + (s.match(/[^\x00-\xff]/g) || "").length;// 加上匹配到的全角字符长度
}

function limit(obj, limit) {
var val = obj.value;
if (len(val) > limit) {
val=val.substring(0,limit);
while (len(val) > limit){
val = val.substring(0, val.length - 1);
};
obj.value = val;
}
}